﻿#region File Description
/****************************************************************************
*                                                                           *
* Game Manager.                                                             *
*                                                                           *
* Copyright (c) Ionescu Marius. All rights reserved(2013).                  *
*                                                                           *
****************************************************************************/
#endregion
namespace Silverlight3D
{
    using System.Collections.Generic;
    using System.Windows.Media;

    /// <summary>
    /// Shades colors according to a brightness factor.
    /// </summary>
    public class Shader : IShader
    {
        private IDictionary<Color, Brush[]> brushCache = new Dictionary<Color, Brush[]>();

        /// <summary>
        /// Shades a given color according to a given brightness factor (between 0 and 1).
        /// </summary>
        /// <param name="color">Color to shade</param>
        /// <param name="brightness">How bright the resulting color should be (between 0 and 1).</param>
        public Color GetShadedColor(Color color, double brightness)
        {
            return color.Multiply(brightness);
        }

        /// <summary>
        /// Shades a given color according to a given brightness factor (between 0 and 1) and returns a Brush set to that color.
        /// </summary>
        /// <param name="color">Color to shade</param>
        /// <param name="brightness">How bright the resulting brush's color should be (between 0 and 1).</param>
        public Brush GetShadedBrush(Color color, double brightness)
        {
            Brush[] brushes;

            if (!this.brushCache.TryGetValue(color, out brushes))
            {
                brushes = new SolidColorBrush[256];

                for (int i = 0; i < 256; i++)
                {
                    brushes[i] = new SolidColorBrush(color.Multiply((double)i / 256));
                }

                this.brushCache.Add(color, brushes);
            }

            return this.brushCache[color][(int)(brightness * 255)];
        }
    }
}